package org.erepublik.company.product;

import org.erepublik.company.Company;
import org.erepublik.company.ECountry;
import org.erepublik.company.EProductType;
import org.json.JSONException;
import org.json.JSONObject;

public class Food extends Product {

    public Food() {
        super(EProductType.FOOD);
    }
    
    public double getWellness() {
        return getProductDesign().getAttributeValue(EProductAttribute.WELLNESS)/10.0d;
    }
    
    public double getHappiness() {
        return getProductDesign().getAttributeValue(EProductAttribute.HAPPINESS)/10.0d;
    }
    
    public double getFoodEfficency() {
        return getWellnessEfficency();

    }
    
    public double getWellnessEfficency() {
        return Math.round( (getWellness()/getPriceInGold())*100.0d )/100.0d ;
    }

    public static Food getFoodFromJSON(JSONObject object) throws JSONException {
        Food product = new Food();
        
        String companyName = "no name";
        if (object.has("companyName"))
            companyName = (String)object.get("companyName");
        product.setCompanyOwner( 
                new Company( (Integer)object.get("companyOwner"), 
                        0, 
                        ECountry.valueOf( (String) object.get("companyCountry") ) ,companyName));
        product.setMarket(ECountry.valueOf( (String) object.get("market") ) );
        if (object.get("price") instanceof Integer)
            product.setPrice( (Integer)object.get("price") );
        else
            product.setPrice( (Double)object.get("price") );
        if (object.get("priceInGold") instanceof Integer)
            product.setPriceInGold( (Integer)object.get("priceInGold") );
        else
            product.setPriceInGold( (Double)object.get("priceInGold") );
        product.setQuantity((Integer)object.get("quantity"));
        product.getProductDesign().setAttributeValue(EProductAttribute.WELLNESS, 
                (Integer)object.get("wellness"));
        product.getProductDesign().setAttributeValue(EProductAttribute.HAPPINESS, 
                (Integer)object.get("happiness"));
        
        return product;
    }
    
    public JSONObject toJSONObject() throws JSONException {
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("companyOwner", getCompanyOwner().getId());
        jsonObj.put("companyCountry", getCompanyOwner().getCountry());
        jsonObj.put("companyName", getCompanyOwner().getName());
        jsonObj.put("market", getCountry().toString());
        jsonObj.put("price", getPrice());
        jsonObj.put("priceInGold", getPriceInGold());
        jsonObj.put("quantity", getQuantity());
        jsonObj.put("wellness", getProductDesign().getAttributeValue(EProductAttribute.WELLNESS));
        jsonObj.put("happiness", getProductDesign().getAttributeValue(EProductAttribute.HAPPINESS));
        return jsonObj;
    }

    public int getQuality() {
        int customizationPoints = getProductDesign().getAttributeValue(EProductAttribute.WELLNESS)
                + getProductDesign().getAttributeValue(EProductAttribute.HAPPINESS);
        return customizationPoints/20;
    }
    
    

}
